package com.dxp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dxp.common.CustomException;
import com.dxp.common.R;
import com.dxp.dto.SetmealDto;
import com.dxp.pojo.Dish;
import com.dxp.pojo.Setmeal;
import com.dxp.pojo.SetmealDish;
import com.dxp.service.DishService;
import com.dxp.service.SetmealDishService;
import com.dxp.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    SetmealService setMealService;
    @Autowired
    SetmealDishService setmealDishService;
    @Autowired
    DishService dishService;

    //新增套餐
    @PostMapping
    //删除这个分类下所有的缓存
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
         setMealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }
    //分页展示
    @GetMapping("/page")
    public R<Page> list(int page, int pageSize, String name) {
        Page page1 = setMealService.listSetMeal(page, pageSize, name);
        return R.success(page1);
    }
    //删除套餐
    @DeleteMapping
    //删除这个分类下所有的缓存
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        setMealService.deleteWithDish(ids);
        return R.success("删除套餐成功");
    }
    //修改套餐，获得该套餐数据
    @GetMapping("/{id}")
    public R<SetmealDto> update(@PathVariable Long id){
        SetmealDto setmealDto = setMealService.getByIdUpdateSetmeal(id);
        return R.success(setmealDto);
    }
    //修改
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setMealService.updateSetmeal(setmealDto);
        return R.success("修改成功");
    }
    //批量停售
    @PostMapping("/status/0")
    public R<String> update(@RequestParam List<Long> ids){
        QueryWrapper<Setmeal> setmealQueryWrapper = new QueryWrapper<>();
        setmealQueryWrapper.in("id",ids);
        List<Setmeal> setmealList = setMealService.list(setmealQueryWrapper);
        setmealList=setmealList.stream().map((item)->{
            item.setStatus(0);
            return item;
        }).collect(Collectors.toList());
        setMealService.updateBatchById(setmealList);
        return R.success("修改成功");
    }
    //批量起售
    @PostMapping("/status/1")
    public R<String> update1(@RequestParam List<Long> ids){
        QueryWrapper<Setmeal> setmealQueryWrapper = new QueryWrapper<>();
        setmealQueryWrapper.in("id",ids);
        List<Setmeal> setmealList = setMealService.list(setmealQueryWrapper);
        setmealList=setmealList.stream().map((item)->{
            Long id = item.getId();
            QueryWrapper<SetmealDish> setmealDishQueryWrapper = new QueryWrapper<>();
            setmealDishQueryWrapper.eq("setmeal_id",id);
            List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishQueryWrapper);
            for (SetmealDish setmealDish : setmealDishList) {
                Long dishId = setmealDish.getDishId();
                Dish dish = dishService.getById(dishId);
                if(dish==null){
                    throw new CustomException("该套餐包含的菜品:"+setmealDish.getName()+"已经被删除,所以该套餐无法起售,请重新编辑套餐");
                }else if(dish.getStatus()==0){
                    throw new CustomException("该套餐包含的菜品:"+setmealDish.getName()+"已经被停售,所以该套餐无法起售,请重新编辑套餐");
                }
            }
            item.setStatus(1);
            return item;
        }).collect(Collectors.toList());
        setMealService.updateBatchById(setmealList);
        return R.success("修改成功");

    }
    //移动端展示套餐数据
    @GetMapping("/list")
    @Cacheable(value="setmealCache",key="#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        QueryWrapper<Setmeal> setmealQueryWrapper = new QueryWrapper<>();
        setmealQueryWrapper.eq("category_id",setmeal.getCategoryId());
        setmealQueryWrapper.eq("status",setmeal.getStatus());
        setmealQueryWrapper.orderByDesc("update_time");
        List<Setmeal> setmealList = setMealService.list(setmealQueryWrapper);
        return R.success(setmealList);
    }
}
